package app.auto.runner.base.utility;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;

import app.auto.AndroidInstance;

/***
 *
 * 分辨率处理类
 * 见Cfg
 */
public class ScreenSchema extends AndroidInstance {
    public static int w;
    public static int h;
    public static double sqrt;

    public static int densityDpi;
    public static float density;
    private static boolean b;
    public static int rawdensityDpi;

    public static void updateMetric(Context context) {
        updateMetricPrivate(context);
    }

    private static void updateMetricPrivate(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        dm.densityDpi = ScreenSchema.rawdensityDpi;
        context.getResources().updateConfiguration(context.getResources().getConfiguration(), dm);
    }


    public static void clear() {
        b = false;
    }

    public static void init(Context ctx) {
        initPrivate(ctx);

    }

    private static void initPrivate(Context ctx) {
        if (!b) {

            b = true;


            ScreenSchema.w = ((Activity) ctx).getWindowManager().getDefaultDisplay()
                    .getWidth();
            ScreenSchema.h = ((Activity) ctx).getWindowManager().getDefaultDisplay()
                    .getHeight();
            ScreenSchema.rawdensityDpi = ScreenSchema.densityDpi = ctx.getResources().getDisplayMetrics().densityDpi;
            ScreenSchema.density = ctx.getResources().getDisplayMetrics().density;
//            ShareAlert.shareAlert(ctx,"densitydpi = "+ctx.getResources().getDisplayMetrics().densityDpi+" width = "+ScreenSchema.w);

            Float densityDpi = new Float(
                    Cfg.densityDpi * new Float((sqrt = Math.sqrt(new Float(ScreenSchema.w * ScreenSchema.w + ScreenSchema.h * ScreenSchema.h))) / Math.sqrt(new Float(Cfg.width * Cfg.width + Cfg.height * Cfg.height))));

            Float density = new Float(densityDpi / 160f);
            ScreenSchema.densityDpi = densityDpi.intValue();

            ScreenSchema.density = density;

        }
        if (ctx.getResources().getDisplayMetrics().densityDpi != ScreenSchema.densityDpi) {
            ctx.getResources().getDisplayMetrics().densityDpi = ScreenSchema.densityDpi;
            ctx.getResources().getDisplayMetrics().density = ScreenSchema.density;
        }
    }

    public static void pop(Context ctx) {
        popPrivate(ctx);

    }

    private static void popPrivate(Context ctx) {
        ShareAlert.shareAlert((Activity) ctx, "dpi " + ctx.getResources().getDisplayMetrics().densityDpi + "\n"
                + "width " + ((Activity) ctx).getWindowManager().getDefaultDisplay().getWidth() + "\n" +
                "height " + ((Activity) ctx).getWindowManager().getDefaultDisplay().getHeight() + "\n");
    }

    public static void initcfg(int width, int height, int dpi) {
        Cfg.densityDpi = dpi;
        Cfg.width = width;
        Cfg.height = height;
    }

    public static void initcfg(int width, int height, int dpi, Context context) {
        Cfg.densityDpi = dpi;
        Cfg.width = width;
        Cfg.height = height;
        ScreenSchema.init(context);
    }

    public static void demo(Context context) {
        int width = 1920;
        int height = 1080;
        int dpi = 480;
        ScreenSchema.initcfg(width, height, dpi);
        ScreenSchema.init(context);
    }

    //2073600
    //6553600
    //8627200
//2937/2203
    public static float getDensity() {
        return density;
    }

    @Override
    public AndroidInstance use(Object mp, Object... p) {
        return null;
    }
}
